package com.bringway.p2p.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.domain.TreeNode;
import com.bringway.framework.domain.TreeNodeCode;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.service.MenuService;
import com.bringway.p2p.domain.Tree;
import com.bringway.p2p.service.CodeService;
import com.bringway.p2p.service.MenuCUDService;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/code")
public class CodeController extends JsonController {
	@Autowired
	private CodeService codeService;

	@RequestMapping("/addMenu")
	public ModelAndView addMenu(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = RequestUtils.getStringParameter(request, "name");
		String code_alias = RequestUtils.getStringParameter(request, "code_alias");
		String code_value = RequestUtils.getStringParameter(request, "code_value");
		String comment = RequestUtils.getStringParameter(request, "comment");
		String order_index = RequestUtils.getStringParameter(request, "order_index");
		String parent_id = RequestUtils.getStringParameter(request, "parent_id");
		JSONObject parms = new JSONObject();
		parms.put("name", name);
		parms.put("code_alias", code_alias);
		parms.put("code_value", code_value);
		parms.put("comment", comment);
		parms.put("order_index", order_index);
		parms.put("parent_id", parent_id);
		boolean bool = codeService.addMenu(parms);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isAddSuc", bool);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/deleteMenu")
	public ModelAndView deleteMenu(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int id = RequestUtils.getIntParameter(request, "id", 0);
		JSONObject parms = new JSONObject();
		parms.put("id", id);
		boolean bool = codeService.deleteMenu(parms);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("suc", bool);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/updateMenu")
	public ModelAndView updateMenu(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = RequestUtils.getStringParameter(request, "name");
		String code_alias = RequestUtils.getStringParameter(request, "code_alias");
		String code_value = RequestUtils.getStringParameter(request, "code_value");
		String comment = RequestUtils.getStringParameter(request, "comment");
		String order_index = RequestUtils.getStringParameter(request, "order_index");
		String parent_id = RequestUtils.getStringParameter(request, "parent_id");
		int id = RequestUtils.getIntParameter(request, "nodeid");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("name", name);
		parms.put("alias", code_alias);
		parms.put("value", code_value);
		parms.put("comment", comment);
		parms.put("index", order_index);
		parms.put("parent_id", parent_id);
		parms.put("id", id);
		boolean bool = codeService.updateMenu(parms);
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/queryMenuList")
	public ModelAndView queryMenuList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String nodeId = RequestUtils.getStringParameter(request, "id");
		String name = RequestUtils.getStringParameter(request, "name");
		String alias = RequestUtils.getStringParameter(request, "alias");
		String comment = RequestUtils.getStringParameter(request, "comment");
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		params.put("start", start);
		params.put("limit", limit);
		params.put("nodeId", nodeId);
		params.put("name", name);
		params.put("alias", alias);
		params.put("comment", comment);
		List<Map<String, Object>> list = codeService.queryMenuList(params);
		int count=codeService.getQueryMenuCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/queryMenuListWithOutCheckBox")
	public ModelAndView queryMenuListWithOutCheckBox(HttpServletRequest request,HttpServletResponse response)throws Exception{
		SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		JSONObject params = new JSONObject();
		String nodeId = request.getParameter("nodeId");
		params.put("nodeId", nodeId);
		params.put("roleId", sysUserDetails.getRoleId());
		params.put("gyh", sysUserDetails.getGyh());
		
		List<Map<String, Object>> list = codeService.queryMenuListWithOutCheckBox(params);
		ArrayList list2 = new ArrayList();
	    Iterator iterator = list.iterator();
	    while(iterator.hasNext()){
	    	Map<String, Object> map = (Map)iterator.next();
	    	iterator.remove();
	    	TreeNodeCode treeNodeCode = new TreeNodeCode();
	    	treeNodeCode.setAsync(true);
	    	treeNodeCode.setId(map.get("ID").toString());
	    	treeNodeCode.setText(map.get("NAME").toString());
	    	treeNodeCode.setAlias(map.get("CODE_ALIAS").toString());
	    	treeNodeCode.setValue(map.get("CODE_VALUE").toString());
	    	treeNodeCode.setComment(map.get("CODE_COMMENT").toString());
	    	treeNodeCode.setIndex(map.get("ORDER_INDEX").toString());
	    	treeNodeCode.setParentId(map.get("PARENT_ID").toString());
	    	treeNodeCode.setChildct(map.get("CHILDCT").toString());
	    	list2.add(treeNodeCode);
	    }
	    serialize(request, response, list2,true);
		return null;
	}
}
